package cn.edu.imust.loginregisterdemo.login.model;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import java.util.Random;

public class Code {

    //验证码内容
    private static final char[] CHARS = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
            'u', 'v', 'w', 'x', 'y', 'z'
    };

    private int width = 130;                //The width of the bitmap
    private int height = 36;                //The height of the bitmap
    private String code;
    private Random random = new Random();
    private int paddingLeft = 15;
    private int paddingTop = 15;
    private static Code bmpCode;
    private StringBuilder buffer;

    public static Code getInstance() {

        if (bmpCode == null) {
            bmpCode = new Code();
        }
        return bmpCode;
    }

    public Bitmap createBitp() {
        paddingLeft = 0;

        Bitmap bp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bp);
        code = createCode();
        //创建画笔
        Paint paint = new Paint();
        canvas.drawColor(Color.WHITE);
        paint.setTextSize(20);
        paint.setAntiAlias(true);
        for (int i = 0; i <code.length() ; i++) {
            randomTextStyle(paint);
            randomPadding();
            canvas.drawText(code.charAt(i) + "", paddingLeft, paddingTop, paint);
        }

        //绘制5条干扰线
        //干扰线数量
        int line_number = 3;
        for (int i = 0; i < line_number; i++) {
            drawLine(canvas, paint);
        }
        canvas.save();          //保存
        canvas.restore();       //把上次绘画的画板和画笔保存一下
        return bp;
    }

    private void drawLine(Canvas canvas, Paint paint) {
        int color = randomColor();
        int startX = random.nextInt(width);
        int startY = random.nextInt(height);
        int stopX = random.nextInt(width);
        int stopY = random.nextInt(height);
        paint.setColor(color);
        paint.setStrokeWidth(1);
        canvas.drawLine(startX, startY, stopX, stopY, paint);
    }

    private void randomPadding() {
        paddingLeft += 15 + random.nextInt(15);
        paddingTop =15 + random.nextInt(15);
    }

    private void randomTextStyle(Paint paint) {
        int color = randomColor();
        paint.setColor(color);
        paint.setFakeBoldText(random.nextBoolean());
        float skewX = random.nextInt(11)/10;
        skewX = random.nextBoolean() ? skewX : -skewX;    //True:负数,False:正数
        paint.setTextSkewX(skewX);                        //负数向右倾斜，正数向左倾斜

    }

    //生成验证码
    private String createCode() {
        buffer = new StringBuilder();
        //生成验证码的个数为4个
        int codeLength = 4;
        for (int i = 0; i < codeLength; i++) {
            buffer.append(CHARS[random.nextInt(CHARS.length)]);
        }
        return buffer.toString();
    }

    private int randomColor() {
        int red = random.nextInt(256);
        int green = random.nextInt(256);
        int blue = random.nextInt(256);
        return Color.rgb(red, green, blue);
    }

    public String getCode() {
        return code;
    }
}
